package mulMan.domain;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;


public class ListOfAudioFiles extends Observable implements Set<AudioFile>, Observer {
	
	public Set<AudioFile> list;
	
	public Set<AudioFile> getAsHashSet() {
		return list;
	}
	
	
	
	
	
	public ListOfAudioFiles() {
		list = new HashSet<AudioFile>();
	}
	public ListOfAudioFiles(Set<AudioFile> audioFiles) {
		list = audioFiles;
		
		for (AudioFile audioFile : audioFiles) {
			audioFile.addObserver(this);
		}
	}
	
	
	
	public boolean remove(int index) {
		boolean result = list.remove(index);
		
		setChanged();
		notifyObservers(result);

		return result;
	}

	public boolean add(AudioFile e) {
		boolean result = list.add(e);
		
		e.addObserver(this);
		
		setChanged();
		notifyObservers();
		
		return result;
	}

	public boolean addAll(Collection<? extends AudioFile> c) {
		boolean result = list.addAll(c);
		
		for (AudioFile e : c) {
			e.addObserver(this);
		}
		
		setChanged();
		notifyObservers();
		
		return result;
	}

	public void clear() {
		list.clear();
	}

	public boolean contains(Object o) {
		return list.contains(o);
	}

	public boolean containsAll(Collection<?> c) {
		return list.containsAll(c);
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public Iterator<AudioFile> iterator() {
		return list.iterator();
	}

	public boolean remove(Object o) {
		AudioFile af = null;
		
		if (!(o instanceof AudioFile)) {
			af = (AudioFile) list.toArray()[(Integer) o];
		} else {
			af = (AudioFile) o;
		}
		
		boolean result = list.remove(af);

		((AudioFile)af).deleteObserver(this);
		
		setChanged();
		notifyObservers(af);
		
		return result;
	}

	public boolean removeAll(Collection<?> c) {
		boolean result = list.removeAll(c);
		
		for (Object o : c) {
			((AudioFile)o).deleteObserver(this);

			setChanged();
			notifyObservers(o);
		}
		
		return result;
	}

	public boolean retainAll(Collection<?> c) {
		return list.retainAll(c);
	}

	public int size() {
		return list.size();
	}

	public Object[] toArray() {
		return list.toArray();
	}

	public <T> T[] toArray(T[] a) {
		return list.toArray(a);
	}



	public void update(Observable o, Object arg) {

		if ((o instanceof AudioFile) && (arg.equals("deleted"))) {
			
			if (this.contains(o)) {
				this.remove(o);
			}
		}
	}
}
